Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use GitHub app auth #13

Merged
merged 3 commits into from
Sep 9, 2024
Merged

Use GitHub app auth #13

merged 3 commits into from
Sep 9, 2024

Conversation

danlivings-dxw
Copy link
Contributor

GitHub encourages the use of the GitHub App workflow when a system needs to interact with the GitHub API on its own behalf, rather than using personal access tokens which need to be regenerated on a regular basis.

This PR provides a basic implementation using the GitHub App workflow to provide the list of repositories for organisations that have the app installed.

Copy link
Contributor

@richpjames richpjames left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, it seems like a much more sustainable way of authorising with github.

package-lock.json Show resolved Hide resolved
.env.example Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved

nunjucks.configure({
autoescape: true,
watch: true,
});

const ACCESS_TOKEN = process.env.GITHUB_PERSONAL_ACCESS_TOKEN;
const httpServer = createServer(async (request, response) => {
if (await OctokitApp.middleware(request, response)) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to abstract this out of the callback passed to createServer into a named function, but that can come in a later PR.

GitHub encourages the use of the GitHub App workflow when a system needs
to interact with the GitHub API on its own behalf, rather than using
personal access tokens which need to be regenerated on a regular basis.

This library exposes the GitHub API in a way that's indended to be used
for this workflow.

See
https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/about-authentication-with-a-github-app
for more information about authentication for GitHub apps, including
when an application should authenticate as itself rather than as a
user.
@danlivings-dxw danlivings-dxw force-pushed the feature/use-github-app-auth branch 2 times, most recently from dd75872 to 0b3309b Compare September 9, 2024 10:20
This sets up the `App` instance and registers a generic set of webhooks
that can be used by the Node HTTP server as middleware.
README.md Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
This setup iterates over all installations of the GitHub app and
displays their repositories in the previously made table.

Iterating over multiple installations rather than specifying it
explicitly will allow us to support repositories we manage in other
organisations by getting them to install Towtruck as a GitHub app in
their organisation settings and granting access to the repositories that
we maintain for them.

However, currently we only want to support single-account installations.
There doesn't seem to be a neat way to get the installation ID from an
account name, so we will use `eachInstallation` to loop (hopefully once)
and just take the first (hopefully only) element from `installations` so
that we can have more meaningful template names in Nunjucks.

We can enforce this one-installation approach through GitHub by
configuring the app to be "Only on this account" when registering the app.
@danlivings-dxw danlivings-dxw merged commit f4c0a5c into main Sep 9, 2024
2 checks passed
@danlivings-dxw danlivings-dxw deleted the feature/use-github-app-auth branch September 9, 2024 13:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants